package jdbc.chapter_Jan_1_2019;

import jdbc.annotation.Column;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseDao<T> {
    private Class clazz;
    private String tableName;

    public BaseDao() {
        ParameterizedType parameterizedType= (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz= (Class) parameterizedType.getActualTypeArguments()[0];
        tableName=clazz.getSimpleName();
    }
    /**查询*/
    public List findAll(){
        List<T> list=new ArrayList<>();
        String sql="select *from "+tableName;
        try {
            ResultSet rs=DjdbcU.selectSql(sql);
            Field[] fields=clazz.getDeclaredFields();
            while (rs.next()){
                T object= (T) clazz.newInstance();
                for (int a=0;a<fields.length;a++){
                    fields[a].setAccessible(true);
                    Object o=rs.getObject(a+1);
                    String type=fields[a].getType().getSimpleName();
                    if (type.equals("Integer")){
                        BigDecimal decimal= (BigDecimal) o;
                        if (decimal!=null){
                            o=decimal.intValue();
                        }
                    }
                    fields[a].set(object,o);
                } list.add(object);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    /**删除*/
    public int dleteSql(Object values){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("DELETE FROM ");
        stringBuffer.append(tableName+" Where ");
        Field[] fields=clazz.getDeclaredFields();
        for (Field field :fields){
            Annotation a1=field.getAnnotation(Column.class);
            if (((Column) a1).Pk()){
                stringBuffer.append(((Column) a1).name()+"=");
                if(values instanceof String)
                stringBuffer.append("'"+values+"'");
                else
                stringBuffer.append(values);
                try {
                    DjdbcU.uptata(stringBuffer.toString());
                    return 0;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;

            }

        }
        return 1;
    }
    /**插入*/
    public int insert(T o)  {
        StringBuffer stringBuffer=new StringBuffer("insert into ");
        stringBuffer.append(tableName+" (");
        List<Object> params=new ArrayList<>();
        Field[] fields=clazz.getDeclaredFields();
        for (int  a=0;a<fields.length;a++){
            String fieldName="";
            fields[a].setAccessible(true);
            try {
                params.add(fields[a].get(o));
            } catch (IllegalAccessException e) {
                return 0;
            }
            Annotation annotation=fields[a].getAnnotation(Column.class);
            if (annotation!=null){
                fieldName=((Column) annotation).name();
            }else {
                fieldName=fields[a].getName();
            }
            stringBuffer.append(fieldName);
            if (a<fields.length-1)stringBuffer.append(",");
            else stringBuffer.append(")");


        }
        stringBuffer.append(" values(");
        for (int a=0;a<fields.length;a++){
            stringBuffer.append("?");
            if (a<fields.length-1)stringBuffer.append(",");
            else stringBuffer.append(")");

        }
        System.out.println(stringBuffer);
        try {
            DjdbcU.uptata(stringBuffer.toString(),params.toArray());
        } catch (SQLException e) {
           return 0;
        }
        return 1;
    }
    /**数据更新*/
    public int upDataById(T o,Object values)  {
        String pk_name="";
        StringBuffer stringBuffer=new StringBuffer("UPDATE ")
                .append(tableName)
                .append(" SET ");
        Field[] fields=clazz.getDeclaredFields();
        List<Object> ao=new ArrayList<>();
        for (int a=0;a<fields.length;a++){
            fields[a].setAccessible(true);
            if (fields[a].getAnnotation(Column.class).Pk()){
                pk_name=fields[a].getAnnotation(Column.class).name()+"=";
                if (values instanceof String) pk_name=pk_name+"'"+values+"'";
                else pk_name=pk_name+values;
            }
            try {
                if (!(fields[a].get(o)==null)){
                    Annotation annotation=fields[a].getAnnotation(Column.class);
                    ao.add(fields[a].get(o));
                    stringBuffer.append(((Column) annotation).name()+"=");
                    stringBuffer.append("?");
                    if (a<fields.length-1)stringBuffer.append(",");
                    else stringBuffer.append(" Where ");
                    }else continue;
            } catch (IllegalAccessException e) {
                return 0;
            }
        }
            stringBuffer.append(pk_name);
        try {
            DjdbcU.uptata(stringBuffer.toString(),ao.toArray());
        } catch (SQLException e) {
            return 0;
        }
        return 1;


    }

}
